#include "RbcpConfig.hpp"
#include "JsonReader.hpp"
#include <unistd.h>

RbcpConfig::RbcpConfig()
{
	//mRbcpConfigData = UdpSR();
	mRbcpConfigData = RbcpConfigData();
	JsonReader j = JsonReader();
	mRbcpConfigData = j.mRbcpConfigData;
	//j.show();
	printf("=================================================\n");
	printf("mIpre is %d\n", mRbcpConfigData.mIpre);
	    printf("mIsha is %d\n", mRbcpConfigData.mIsha);
	    printf("mIbuf is %d\n", mRbcpConfigData.mIbuf);
	    printf("mIcomp is %d\n", mRbcpConfigData.mIcomp);
	    printf("mIdriver is %d\n", mRbcpConfigData.mIdriver);
	    printf("mVfp is %d\n", mRbcpConfigData.mVfp);
	    printf("mVfs is %d\n", mRbcpConfigData.mVfs);
	    printf("mVref0 is %d\n", mRbcpConfigData.mVref0);
	    printf("mVref1 is %d\n", mRbcpConfigData.mVref1);
	    printf("mVoffset is %d\n", mRbcpConfigData.mVoffset);
	    printf("mAout is %d\n", mRbcpConfigData.mAout);
	    printf("mTpReg is %d\n", mRbcpConfigData.mTpReg);
	    printf("mCReg is %d\n", mRbcpConfigData.mCReg);
	    printf("testMode is %s\n", mRbcpConfigData.testMode?"true":"false");
	    printf("multiHitFilter is %s\n", mRbcpConfigData.multiHitFilter?"true":"false");
	    printf("=================================================\n");


}
RbcpConfig::RbcpConfig(int mIpre, int mIsha, int mIbuf, int mIcomp, int mIdriver, int mVfp, int mVfs, int mVref0, int mVref1, int mVoffset, int mAout, int mTpReg, int mCReg) {
	mRbcpConfigData = RbcpConfigData();
	mRbcpConfigData.mIpre = mIpre;
	mRbcpConfigData.mIsha = mIsha;
	mRbcpConfigData.mIbuf = mIbuf;
	mRbcpConfigData.mIcomp = mIcomp;
	mRbcpConfigData.mIdriver = mIdriver;
	mRbcpConfigData.mVfp = mVfp;
	mRbcpConfigData.mVfs = mVfs;
	mRbcpConfigData.mVref0 = mVref0;
	mRbcpConfigData.mVref1 = mVref1;
	mRbcpConfigData.mVoffset = mVoffset;
	mRbcpConfigData.mAout = mAout;
	mRbcpConfigData.mTpReg = mTpReg;
	mRbcpConfigData.mCReg = mCReg;
		JsonReader j = JsonReader();
		//j.show();
		mRbcpConfigData = j.mRbcpConfigData;
		printf("RbcpConfig------------------------------------mIpre is %d\n", mRbcpConfigData.mIpre);
	//j.readJSONFile();
}
RbcpConfig::~RbcpConfig() {

}
void RbcpConfig::IpreConfig() {
	char IpreContent[256] = {0x0, 0xf8, 0x1, 0x1, mRbcpConfigData.mIpre, 0x0, 0x1, 0x0};
	mUdpSR.rbcp("wrb", "0x1f", &IpreContent[0]);
	usleep(100000);
	mUdpSR.rbcp("wrb", "0x1a", &IpreContent[1]);
	mUdpSR.rbcp("wrb", "0x1b", &IpreContent[2]);
	mUdpSR.rbcp("wrb", "0x1c", &IpreContent[3]);
	mUdpSR.rbcp("wrb", "0x1d", &IpreContent[4]);
	mUdpSR.rbcp("wrb", "0x20", &IpreContent[5]);
	mUdpSR.rbcp("wrb", "0x20", &IpreContent[6]);
	mUdpSR.rbcp("wrb", "0x20", &IpreContent[7]);
}
void RbcpConfig::IshaConfig() {
	char IshaContent[256] = {0xf8, 0x1, 0x2, mRbcpConfigData.mIsha, 0x0, 0x1, 0x0};
	mUdpSR.rbcp("wrb", "0x1a", &IshaContent[0]);
	mUdpSR.rbcp("wrb", "0x1b", &IshaContent[1]);
	mUdpSR.rbcp("wrb", "0x1c", &IshaContent[2]);
	mUdpSR.rbcp("wrb", "0x1d", &IshaContent[3]);//edit
	mUdpSR.rbcp("wrb", "0x20", &IshaContent[4]);
	mUdpSR.rbcp("wrb", "0x20", &IshaContent[5]);
	mUdpSR.rbcp("wrb", "0x20", &IshaContent[6]);
}
void RbcpConfig::IbufConfig() {
		char IbufContent[256] = {0xf8, 0x1, 0x3, mRbcpConfigData.mIbuf, 0x0, 0x1, 0x0};
	mUdpSR.rbcp("wrb", "0x1a", &IbufContent[0]);
	mUdpSR.rbcp("wrb", "0x1b", &IbufContent[1]);
	mUdpSR.rbcp("wrb", "0x1c", &IbufContent[2]);
	mUdpSR.rbcp("wrb", "0x1d", &IbufContent[3]);
	mUdpSR.rbcp("wrb", "0x20", &IbufContent[4]);
	mUdpSR.rbcp("wrb", "0x20", &IbufContent[5]);
	mUdpSR.rbcp("wrb", "0x20", &IbufContent[6]);

}
void RbcpConfig::IcompConfig() {
	char IcompContent[256] = {0xf8, 0x1, 0x4, mRbcpConfigData.mIcomp, 0x0, 0x1, 0x0};
	mUdpSR.rbcp("wrb", "0x1a", &IcompContent[0]);
	mUdpSR.rbcp("wrb", "0x1b", &IcompContent[1]);
	mUdpSR.rbcp("wrb", "0x1c", &IcompContent[2]);
	mUdpSR.rbcp("wrb", "0x1d", &IcompContent[3]);
	mUdpSR.rbcp("wrb", "0x20", &IcompContent[4]);
	mUdpSR.rbcp("wrb", "0x20", &IcompContent[5]);
	mUdpSR.rbcp("wrb", "0x20", &IcompContent[6]);
}
void RbcpConfig::IdriverConfig() {
	char IdriverContent[256] = {0xf8, 0x1, 0x5, mRbcpConfigData.mIdriver, 0x0, 0x1, 0x0};
	mUdpSR.rbcp("wrb", "0x1a", &IdriverContent[0]);
	mUdpSR.rbcp("wrb", "0x1b", &IdriverContent[1]);
	mUdpSR.rbcp("wrb", "0x1c", &IdriverContent[2]);
	mUdpSR.rbcp("wrb", "0x1d", &IdriverContent[3]);
	mUdpSR.rbcp("wrb", "0x20", &IdriverContent[4]);
	mUdpSR.rbcp("wrb", "0x20", &IdriverContent[5]);
	mUdpSR.rbcp("wrb", "0x20", &IdriverContent[6]);
}
void RbcpConfig::VfpConfig() {
	char VfpContent[256] = {0xf8, 0x1, 0x6, mRbcpConfigData.mVfp, 0x0, 0x1, 0x0};
	mUdpSR.rbcp("wrb", "0x1a", &VfpContent[0]);
	mUdpSR.rbcp("wrb", "0x1b", &VfpContent[1]);
	mUdpSR.rbcp("wrb", "0x1c", &VfpContent[2]);
	mUdpSR.rbcp("wrb", "0x1d", &VfpContent[3]);
	mUdpSR.rbcp("wrb", "0x20", &VfpContent[4]);
	mUdpSR.rbcp("wrb", "0x20", &VfpContent[5]);
	mUdpSR.rbcp("wrb", "0x20", &VfpContent[6]);

}
void RbcpConfig::VfsConfig() {
	char VfsContent[256] = {0xf8, 0x1, 0x7, mRbcpConfigData.mVfs, 0x0, 0x1, 0x0};
	mUdpSR.rbcp("wrb", "0x1a", &VfsContent[0]);
	mUdpSR.rbcp("wrb", "0x1b", &VfsContent[1]);
	mUdpSR.rbcp("wrb", "0x1c", &VfsContent[2]);
	mUdpSR.rbcp("wrb", "0x1d", &VfsContent[3]);
	mUdpSR.rbcp("wrb", "0x20", &VfsContent[4]);
	mUdpSR.rbcp("wrb", "0x20", &VfsContent[5]);
	mUdpSR.rbcp("wrb", "0x20", &VfsContent[6]);

}
void RbcpConfig::Vref0Config() {
	char Vref0Content[256] = {0xf8, 0x1, 0x8, mRbcpConfigData.mVref0, 0x0, 0x1, 0x0};
	mUdpSR.rbcp("wrb", "0x1a", &Vref0Content[0]);
	mUdpSR.rbcp("wrb", "0x1b", &Vref0Content[1]);
	mUdpSR.rbcp("wrb", "0x1c", &Vref0Content[2]);
	mUdpSR.rbcp("wrb", "0x1d", &Vref0Content[3]);
	mUdpSR.rbcp("wrb", "0x20", &Vref0Content[4]);
	mUdpSR.rbcp("wrb", "0x20", &Vref0Content[5]);
	mUdpSR.rbcp("wrb", "0x20", &Vref0Content[6]);

}
void RbcpConfig::Vref1Config() {
	char Vref1Content[256] = {0xf8, 0x1, 0x9, mRbcpConfigData.mVref1, 0x0, 0x1, 0x0};
	mUdpSR.rbcp("wrb", "0x1a", &Vref1Content[0]);
	mUdpSR.rbcp("wrb", "0x1b", &Vref1Content[1]);
	mUdpSR.rbcp("wrb", "0x1c", &Vref1Content[2]);
	mUdpSR.rbcp("wrb", "0x1d", &Vref1Content[3]);
	mUdpSR.rbcp("wrb", "0x20", &Vref1Content[4]);
	mUdpSR.rbcp("wrb", "0x20", &Vref1Content[5]);
	mUdpSR.rbcp("wrb", "0x20", &Vref1Content[6]);

}
void RbcpConfig::VoffsetConfig() {
	char VoffsetContent[256] = {0xf8, 0x1, 0xa, mRbcpConfigData.mVoffset, 0x0, 0x1, 0x0};
	mUdpSR.rbcp("wrb", "0x1a", &VoffsetContent[0]);
	mUdpSR.rbcp("wrb", "0x1b", &VoffsetContent[1]);
	mUdpSR.rbcp("wrb", "0x1c", &VoffsetContent[2]);
	mUdpSR.rbcp("wrb", "0x1d", &VoffsetContent[3]);
	mUdpSR.rbcp("wrb", "0x20", &VoffsetContent[4]);
	mUdpSR.rbcp("wrb", "0x20", &VoffsetContent[5]);
	mUdpSR.rbcp("wrb", "0x20", &VoffsetContent[6]);

}
void RbcpConfig::AoutConfig() {
	#if 0
	char AoutContent[256] = {0xf8, 0x1, 0xc, mRbcpConfigData.mAout, 0x0, 0x1, 0x0};
	mUdpSR.rbcp("wrb", "0x1a", &AoutContent[0]);
	mUdpSR.rbcp("wrb", "0x1b", &AoutContent[1]);
	mUdpSR.rbcp("wrb", "0x1c", &AoutContent[2]);
	mUdpSR.rbcp("wrb", "0x1d", &AoutContent[3]);
	mUdpSR.rbcp("wrb", "0x20", &AoutContent[4]);
	mUdpSR.rbcp("wrb", "0x20", &AoutContent[5]);
	mUdpSR.rbcp("wrb", "0x20", &AoutContent[6]);
	#endif
}


void RbcpConfig::TpRegConfig() {

	char TpRegContent[256] = {0xf8, 0x1, 0xc, mRbcpConfigData.mTpReg, 0x0, 0x1, 0x0};
	mUdpSR.rbcp("wrb", "0x1a", &TpRegContent[0]);
	mUdpSR.rbcp("wrb", "0x1b", &TpRegContent[1]);
	mUdpSR.rbcp("wrb", "0x1c", &TpRegContent[2]);
	mUdpSR.rbcp("wrb", "0x1d", &TpRegContent[3]);
	mUdpSR.rbcp("wrb", "0x20", &TpRegContent[4]);
	mUdpSR.rbcp("wrb", "0x20", &TpRegContent[5]);
	mUdpSR.rbcp("wrb", "0x20", &TpRegContent[6]);

}
void RbcpConfig::CRegConfig  () {
	char CRegContent[256] = {0xf8, 0x1, 0xd, mRbcpConfigData.mCReg, 0x0, 0x1, 0x0};
	mUdpSR.rbcp("wrb", "0x1a", &CRegContent[0]);
	mUdpSR.rbcp("wrb", "0x1b", &CRegContent[1]);
	mUdpSR.rbcp("wrb", "0x1c", &CRegContent[2]);
	mUdpSR.rbcp("wrb", "0x1d", &CRegContent[3]);
	mUdpSR.rbcp("wrb", "0x20", &CRegContent[4]);
	mUdpSR.rbcp("wrb", "0x20", &CRegContent[5]);
	mUdpSR.rbcp("wrb", "0x20", &CRegContent[6]);

}

void RbcpConfig::startRealityMode() {
	testModeOff();
	multiHitFilter();
	ready();
	start();

}
void RbcpConfig::configCommit() {
	if (mRbcpConfigData.testMode)
	{
		testModeOn();
		printf("=====================testModeOn ====================\n");
		/* code */
	} else {
		testModeOff();
		printf("=====================testModeOff ====================\n");
		/* code */

	}
	usleep(100000);
	multiHitFilter();
	usleep(100000);
	ready();

}
void RbcpConfig::start() {
	char CRegContent[256] = {0x01};
	mUdpSR.rbcp("wrb", "0x22", &CRegContent[0]);
}

void RbcpConfig::reset() {
	char CRegContent[256] = {0x0, 0x01, 0x0};
	mUdpSR.rbcp("wrb", "0x21", &CRegContent[0]);
	usleep(100000);
	mUdpSR.rbcp("wrb", "0x21", &CRegContent[1]);
	usleep(100000);
	mUdpSR.rbcp("wrb", "0x21", &CRegContent[2]);
	usleep(100000);
}

void RbcpConfig::stop() {
	char CRegContent[256] = {0x0, 0x01, 0x0};
	mUdpSR.rbcp("wrb", "0x22", &CRegContent[0]);
}

void RbcpConfig::testModeOn() {
	char CRegContent[256] = {0x01};
	mUdpSR.rbcp("wrb", "0x24", &CRegContent[0]);
}

void RbcpConfig::testModeOff() {
	char CRegContent[256] = {0x00};
	mUdpSR.rbcp("wrb", "0x24", &CRegContent[0]);
}

void RbcpConfig::multiHitFilter() {
	char CRegContent[256] = {0x01};
	mUdpSR.rbcp("wrb", "0x23", &CRegContent[0]);
}

void RbcpConfig::ready() {
	#define ready_sleep_time 300000
	IpreConfig();
	usleep(ready_sleep_time);
	IshaConfig();
	usleep(ready_sleep_time);
	IbufConfig();
	usleep(ready_sleep_time);
	IcompConfig();
	usleep(ready_sleep_time);
	IdriverConfig();
	usleep(ready_sleep_time);
	VfpConfig();
	usleep(ready_sleep_time);
	VfsConfig();
	usleep(ready_sleep_time);
	Vref0Config();
	usleep(ready_sleep_time);
	Vref1Config();
	usleep(ready_sleep_time);
	VoffsetConfig();
	usleep(ready_sleep_time);
	AoutConfig();
	usleep(ready_sleep_time);
	TpRegConfig();
	usleep(ready_sleep_time);
	CRegConfig ();
	usleep(ready_sleep_time);
}
